package afcp.alumni.email;

import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Properties;

import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import afcp.alumni.action.base.BaseAction;
import afcp.alumni.util.Conf;

import com.google.apphosting.api.ApiProxy.OverQuotaException;

public class Email {

	private static final Log logger = LogFactory.getLog(Email.class);

	private static final Session session = Session.getDefaultInstance(
			new Properties(), null);

	MimeMessage msg = new MimeMessage(session);
	private static final String adminEmail = Conf.get("admin");
//	private static final String adminName = "Administrateur";

	private boolean initialized = false;

	public Email() {
	}

	public void init(String recipientEmail, String recipientName,
			String subject, String bodyHtml, String bodyTxts, Locale locale) throws MessagingException {
		String adminName = BaseAction.msg(locale, "email.password.adminname");
		
		if (recipientEmail == null) {
			throw new NullPointerException("Recipient email cannot be null.");
		}
		
		InternetAddress from;
		InternetAddress to;
		try {
			from = new InternetAddress(adminEmail, adminName,
					Conf.DEFAULT_CHARSET_NAME);
			to = new InternetAddress(recipientEmail, recipientName,
					Conf.DEFAULT_CHARSET_NAME);
		} catch (UnsupportedEncodingException e) {
			logger.error("Incorrect encoding in configuration. "
					+ "The email may have encoding issues.", e);
			try {
				from = new InternetAddress(adminEmail, adminName);
				to = new InternetAddress(recipientEmail, recipientName);
			} catch (UnsupportedEncodingException e1) {
				// There is no reason to get an issue with java default charset.
				from = null;
				to = null;
			}
		}
		msg.setFrom(from);
		msg.addRecipient(RecipientType.TO, to);
		msg.setSubject(subject, Conf.DEFAULT_CHARSET_NAME);
		
		// TODO Va être enlevé
//		msg.setText(bodyHtml, Conf.DEFAULT_CHARSET_NAME, "html");
		
		// Create an "Alternative" Multipart message
		MimeMultipart mp = new MimeMultipart("alternative");

		// Read text file, load it into a BodyPart, and add it to the
		// message.
		MimeBodyPart bp1 = new MimeBodyPart();
		bp1.setContent(bodyTxts, "text/plain");
		mp.addBodyPart(bp1);
		
		// Do the same with the HTML part
		 MimeBodyPart bp2 = new MimeBodyPart();
		 bp2.setContent(bodyHtml, "text/html");
		 mp.addBodyPart(bp2);
		 
		 msg.setContent(mp);

		initialized = true;
	}

	public void send() throws MessagingException, OverQuotaException {
		if (!initialized) {
			throw new IllegalStateException("must initialize "
					+ "the email before sending it. Please call "
					+ "the method init() before send().");
		}
		Transport.send(msg);
		// To simulate the case when there is no quota left.
//		throw new OverQuotaException("afcp.alumni.email.Email", "send");
	}
}
